package com.anolesoft.twelveFactory.planManagement.service.impl;

import com.anolesoft.twelveFactory.planManagement.dto.PlanissuedDto;
import com.anolesoft.twelveFactory.planManagement.entity.PlanIssued;
import com.anolesoft.twelveFactory.planManagement.entity.Process;
import com.anolesoft.twelveFactory.planManagement.mapper.PlanEmployeeMapper;
import com.anolesoft.twelveFactory.planManagement.mapper.PlanIssuedMapper;
import com.anolesoft.twelveFactory.planManagement.mapper.ProcessMapper;
import com.anolesoft.twelveFactory.planManagement.service.PlanIsssuedService;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PlanIsssuedServiceImpl implements PlanIsssuedService {
    @Resource
    private PlanIssuedMapper planIssuedMapper;
    @Resource
    private PlanEmployeeMapper planEmployeeMapper;
    @Resource
    private ProcessMapper processMapper;

    // 查询数据库计划下达表的所有考核日期
    @Override
    public List<PlanIssued> listPlanIssued2() {
        return this.planIssuedMapper.listPlanIssued2();
    }

    //筛选考核日期条件之后的数据 查询计划下达表
    @Override
    public PageData<List<PlanIssued>> listPlanIssued(PlanissuedDto planissuedDto) {
        Page<PlanIssued> page = new Page<>(planissuedDto.getPageNo(), planissuedDto.getPageSize());

//        全查 不带条件
        List<PlanIssued> listPlanIssueds1 = this.planIssuedMapper.listPlanIssued2();

        //        全查 带条件
        List<PlanIssued> listPlanIssueds = this.planIssuedMapper.listPlanIssued(planissuedDto, page);
        //修改计划标记为修改
         List<Process> process = this.processMapper.select();
        for (int i = 0; i < process.size(); i++) {
            if (process.get(i).getXgzt() != null) {
                if (process.get(i).getXgzt() == 1) {
                    List<PlanIssued> planIssued = this.planIssuedMapper.selectByObjuidgyjh(process.get(i).getObjuid());
                    for (int j = 0; j <planIssued.size(); j++) {
                        if(planIssued.get(i)!=null){
                            if (planIssued.get(i).getJhzt().equals("待审核")) {
                                planIssued.get(i).setJhbj("修改");
                                this.planIssuedMapper.Update(planIssued.get(i));
                            }
                        }
                    }
                }
            }
        }


        //序号序号序号!!!!
        // 父图号为空
        List<PlanIssued> roots = listPlanIssueds1.stream().filter(l -> l.getFth()==null).collect(Collectors.toList());

//        符合条件的集合
        List<PlanIssued> planIssueds = new ArrayList<>();
        for (int i = 0; i < listPlanIssueds.size(); i++) {
            PlanIssued p = this.planIssuedMapper.selectByWYZD(((PlanIssued)listPlanIssueds.get(i)).getWyzd(),listPlanIssueds.get(i).getKhrq());
            while(true) {
                if (p != null) {
                    planIssueds.add(p);
                }

                if (p == null || p.getFth() == null) {
                    break;
                }

                p = this.planIssuedMapper.selectByTh(p.getFth(),p.getKhrq());
            }
        }
        //去重
        List<PlanIssued> planIssueds1 = planIssueds.stream().distinct().collect(Collectors.toList());

        for (int i = 0; i <planIssueds1.size(); i++) {
            Calendar c=Calendar.getInstance();
            c.setTime(planIssueds1.get(i).getKhrq());
            c.add(Calendar.MONTH,-1);
            Format f=new SimpleDateFormat();
            Date khrq = new java.sql.Date(c.getTime().getTime());
            PlanIssued p = this.planIssuedMapper.selectByWYZD(((PlanIssued)planIssueds1.get(i)).getWyzd(),khrq);
            if(p!=null){
                planIssueds1.get(i).setBz(p.getBz());
                planIssueds1.get(i).setBcsm(p.getBcsm());
                planIssueds1.get(i).setFzr(p.getFzr());
                planIssueds1.get(i).setWcxz(p.getWcxz());
                planIssueds1.get(i).setCp(p.getCp());
                planIssueds1.get(i).setGj(p.getGj());
                planIssueds1.get(i).setSb(p.getSb());
                planIssueds1.get(i).setJszl(p.getJszl());
                this.planIssuedMapper.Update(planIssueds1.get(i));
            } 
        }
//        过滤 filter(Objects::nonNull).
//        List<PlanIssued> planIssueds1 = planIssueds.stream().distinct().collect(Collectors.toList());

//        得到id
        List<String> id = planIssueds.stream().map(PlanIssued::getObjuid).collect(Collectors.toList());

//所有的id和父图号为空的id对比 集合
        roots = roots.stream().filter(l -> {
            return id.contains(l.getObjuid());
        }).collect(Collectors.toList());

//        给children集合赋值
        roots.forEach(root -> {
            PlanIssued.setChildren(planIssueds, root);
        });

//        sort() 方法用于对数组的元素进行排序。
        roots.sort((e1, e2) -> {
            return -e1.compareTo(e2);
        });



//给code赋值
        for (int i = 0; i < roots.size(); i++) {
//            码  .前
            String code = String.valueOf(i + 1);
            roots.get(i).setCode(code);
            List<PlanIssued> children = roots.get(i).getChildren();
            if (children != null && !children.isEmpty()) {
                //            码  .前+后
                PlanIssued.setChildrenCode(children, code);
            }
        }

        return new PageData<>(((int) page.getTotal()), roots);
    }


    @Override
    public PageData<List<PlanIssued>> listToThePlan(PlanissuedDto planissuedDto) {
        Page<PlanIssued> page = new Page<>(planissuedDto.getPageNo(), planissuedDto.getPageSize());
        List<PlanIssued> listPlanIssueds = planIssuedMapper.listToThePlan(planissuedDto, page);
        //序号序号序号!!!!

        //全部 没有条件
        List<PlanIssued> list = this.planIssuedMapper.listToThePlan2();
        // 父图号为空
        List<PlanIssued> roots = list.stream().filter(l -> l.getFth() == null).collect(Collectors.toList());

        List<PlanIssued> planIssueds = new ArrayList<>();
        for (int i = 0; i < listPlanIssueds.size(); i++) {
            PlanIssued p = this.planIssuedMapper.selectByWYZD1(listPlanIssueds.get(i).getWyzd(),listPlanIssueds.get(i).getKhrq());
            while (1 == 1) {
                if(p!=null){  planIssueds.add(p);}
                if (p == null || p.getFth() == null) {
                    break;
                }
                p = this.planIssuedMapper.selectByTh1(p.getFth(),p.getKhrq());
            }
        }


//        过滤
//        List<PlanIssued> planIssueds1 = planIssueds.stream().distinct().collect(Collectors.toList());
//        得到id
        List<String> id = planIssueds.stream().map(PlanIssued::getObjuid).collect(Collectors.toList());

        roots = roots.stream().filter(l -> {
            return id.contains(l.getObjuid());
        }).collect(Collectors.toList());

        roots.forEach(root -> {
            PlanIssued.setChildren(planIssueds, root);
        });

//        sort() 方法用于对数组的元素进行排序。
        roots.sort((e1, e2) -> {
            return -e1.compareTo(e2);
        });

        for (int i = 0; i < roots.size(); i++) {
//            码  .前
            String code = String.valueOf(i + 1);
            roots.get(i).setCode(code);
            List<PlanIssued> children = roots.get(i).getChildren();
            if (children != null && !children.isEmpty()) {
                //            码  .前+后
                PlanIssued.setChildrenCode(children, code);
            }
        }
        return new PageData<>(((int) page.getTotal()), roots);
    }

    @Override
    public PageData<List<PlanIssued>> listBayPlan(PlanissuedDto planissuedDto) {
        Page<PlanIssued> page = new Page<>(planissuedDto.getPageNo(), planissuedDto.getPageSize());
        List<PlanIssued> listPlanIssueds = planIssuedMapper.listBayPlan(planissuedDto, page);


        //  序号序号序号!!!!
//        全部 没有条件
        List<PlanIssued> list = this.planIssuedMapper.listBayPlan2();
        // 父图号为空
        List<PlanIssued> roots = list.stream().filter(l -> l.getFth() == null).collect(Collectors.toList());

        List<PlanIssued> planIssueds = new ArrayList<>();
        for (int i = 0; i < listPlanIssueds.size(); i++) {
            PlanIssued p = this.planIssuedMapper.selectByWYZD2(listPlanIssueds.get(i).getWyzd(),listPlanIssueds.get(i).getKhrq());
            while (1 == 1) {
                if(p!=null){  planIssueds.add(p);}
                if (p == null || p.getFth() == null) {
                    break;
                }
                p = this.planIssuedMapper.selectByTh2(p.getFth(),p.getKhrq());
            }
        }
//        过滤
//        List<PlanIssued> planIssueds1 = planIssueds.stream().distinct().collect(Collectors.toList());


//        得到id
        List<String> id = planIssueds.stream().map(PlanIssued::getObjuid).collect(Collectors.toList());

        roots = roots.stream().filter(l -> {
            return id.contains(l.getObjuid());
        }).collect(Collectors.toList());

        roots.forEach(root -> {
            PlanIssued.setChildren(planIssueds, root);
        });

//        sort() 方法用于对数组的元素进行排序。
        roots.sort((e1, e2) -> {
            return -e1.compareTo(e2);
        });

        for (int i = 0; i < roots.size(); i++) {
//            码  .前
            String code = String.valueOf(i + 1);
            roots.get(i).setCode(code);
            List<PlanIssued> children = roots.get(i).getChildren();
            if (children != null && !children.isEmpty()) {
                //            码  .前+后
                PlanIssued.setChildrenCode(children, code);
            }
        }

        return new PageData<>(((int) page.getTotal()), roots);
    }

//    修改状态为待审核
    @Override
    public Integer UpdateState(List<PlanissuedDto> planissuedDto) {
        int count = 0;
        List<PlanIssued> planIssueds = this.planIssuedMapper.listPlanIssued2();//全部
//        根据考核日期判断计划标记是否新增
        boolean boo = false;
        for (int j = 0; j < planIssueds.size(); j++) {
            if (planissuedDto.get(0).getKhrq().equals(planIssueds.get(j).getKhrq())) {
                boo = true;
            }
        }


        for (int i = 0; i < planissuedDto.size(); i++) {
            java.sql.Date jhwc2 = null;
            java.sql.Date sjwc2 = null;
            java.sql.Date khrq = null;
            try {
                if (planissuedDto.get(i).getJhwc() != null) {
                    jhwc2 = new java.sql.Date(planissuedDto.get(i).getJhwc().getTime());
                }
                if (planissuedDto.get(i).getSjwc() != null) {
                    sjwc2 = new java.sql.Date(planissuedDto.get(i).getSjwc().getTime());
                }
                if (planissuedDto.get(i).getKhrq() != null) {
                    khrq = new java.sql.Date(planissuedDto.get(i).getKhrq().getTime());
                }
                planissuedDto.get(i).setJhwc(jhwc2);
                planissuedDto.get(i).setSjwc(sjwc2);
                planissuedDto.get(i).setKhrq(khrq);

            } catch (Exception e) {
                e.printStackTrace();
            }
            if (boo) {
                planissuedDto.get(i).setJhbj("新增");
            }
            count += this.planIssuedMapper.Update(planissuedDto.get(i));
        }
        for (int i = 0; i < planissuedDto.size(); i++) {
            PlanIssued planIssued = new PlanIssued();
            String a=planissuedDto.get(i).getClph()==null?"":planissuedDto.get(i).getClph();
            String b=planissuedDto.get(i).getClgg()==null?"":planissuedDto.get(i).getClgg();
            if(a.equals("")&&!b.equals("")){
                planIssued.setXl(b);
            }
            else if(!a.equals("")&&b.equals("")){
                planIssued.setXl(a);
            }
            else if(a.equals("")&&b.equals("")){
                planIssued.setXl("");
            }
            else {
                planIssued.setXl(a + "-" + b);
            }
            planIssued.setJhbj(planissuedDto.get(i).getJhbj());
            planIssued.setJfcj(planissuedDto.get(i).getJfcj());
            planIssued.setJscj(planissuedDto.get(i).getJscj());
            planIssued.setJhwc(planissuedDto.get(i).getJhwc());
            planIssued.setSjwc(planissuedDto.get(i).getSjwc());
            planIssued.setJflx(planissuedDto.get(i).getJflx());
            planIssued.setLjlx(planissuedDto.get(i).getLjlx());
            planIssued.setJfsx(planissuedDto.get(i).getJfsx());
            planIssued.setKhrq(planissuedDto.get(i).getKhrq());
            planIssued.setJhzt(planissuedDto.get(i).getJhzt());
            planIssued.setWg(planissuedDto.get(i).getDzjdh());
            planIssued.setJx(planissuedDto.get(i).getJx());

            planIssued.setDjs(planissuedDto.get(i).getDjs());
            planIssued.setTh(planissuedDto.get(i).getTh());
            planIssued.setFth(planissuedDto.get(i).getFth());
            planIssued.setNbfg(planissuedDto.get(i).getNbfg());
            planIssued.setGylx(planissuedDto.get(i).getGylx());

            planIssued.setLjlx(planissuedDto.get(i).getLjlx());
            planIssued.setObjuidgyjh(planissuedDto.get(i).getObjuid());
            planIssued.setLd(planissuedDto.get(i).getLd());
            planIssued.setPt(planissuedDto.get(i).getPt());
            planIssued.setJd(planissuedDto.get(i).getJd());
            planIssued.setGskhrq(planissuedDto.get(i).getGskhrq());
            planIssued.setFz(planissuedDto.get(i).getFz());
            planIssued.setSwgx(planissuedDto.get(i).getSwgx());
            planIssued.setDjs(planissuedDto.get(i).getDjs());
            planIssued.setMlkczs(planissuedDto.get(i).getMlkczs());
//            planIssued.setJhzt("待审核");
            if (planissuedDto.get(i).getFth()==null){
                planIssued.setWyzd(planissuedDto.get(i).getJx()+",,"+planissuedDto.get(i).getTh()+","+planissuedDto.get(i).getJfcj()+"-"+planissuedDto.get(i).getJscj());
            }
            else {
                planIssued.setWyzd(planissuedDto.get(i).getJx()+",,"+planissuedDto.get(i).getFth()+"::"+planissuedDto.get(i).getTh()+","+planissuedDto.get(i).getJfcj()+"-"+planissuedDto.get(i).getJscj());
            }
            this.planIssuedMapper.insert(planIssued);
        }
        return count;
    }

    //    修改状态为审核通过
    @Override
    public Integer UpdateState1(List<PlanissuedDto> planissuedDto) {
        int count = 0;
        List<Process> processes = this.processMapper.select();
        for (int i = 0; i < planissuedDto.size(); i++) {
            java.sql.Date jfrq2 = null;
            java.sql.Date wcrq2 = null;
            java.sql.Date gdwjrq2 = null;
            java.sql.Date jhwc2 = null;
            java.sql.Date sjwc2 = null;
            try {
                if (planissuedDto.get(i).getJfrq() != null) {
                    jfrq2 = new java.sql.Date(planissuedDto.get(i).getJfrq().getTime());
                }
                if (planissuedDto.get(i).getWcrq() != null) {
                    wcrq2 = new java.sql.Date(planissuedDto.get(i).getWcrq().getTime());
                }
                if (planissuedDto.get(i).getGdwjrq() != null) {
                    gdwjrq2 = new java.sql.Date(planissuedDto.get(i).getGdwjrq().getTime());
                }
                if (planissuedDto.get(i).getJhwc() != null) {
                    jhwc2 = new java.sql.Date(planissuedDto.get(i).getJhwc().getTime());
                }
                if (planissuedDto.get(i).getSjwc() != null) {
                    sjwc2 = new java.sql.Date(planissuedDto.get(i).getSjwc().getTime());
                }
                planissuedDto.get(i).setJfrq(jfrq2);
                planissuedDto.get(i).setWcrq(wcrq2);
                planissuedDto.get(i).setGdwjrq(gdwjrq2);
                planissuedDto.get(i).setJhwc(jhwc2);
                planissuedDto.get(i).setSjwc(sjwc2);
//                planissuedDto.get(i).setObjuidgyjh(processes.get(i).getObjuid());
            } catch (Exception e) {
                e.printStackTrace();
            }
            count += this.planIssuedMapper.Update(planissuedDto.get(i));
        }
        return count;
    }

    @Override
    public Integer Update(PlanissuedDto planissuedDto) {
        //            java.util.date转换成java.sql.date
        java.sql.Date jfrq2 = null;
        java.sql.Date wcrq2 = null;
        java.sql.Date gdwjrq2 = null;
        java.sql.Date jhwc2 = null;
        java.sql.Date sjwc2 = null;
        java.sql.Date jhkg2 = null;
        try {
            if (planissuedDto.getJfrq() != null) {
                jfrq2 = new java.sql.Date(planissuedDto.getJfrq().getTime());
            }
            if (planissuedDto.getWcrq() != null) {
                wcrq2 = new java.sql.Date(planissuedDto.getWcrq().getTime());
            }
            if (planissuedDto.getGdwjrq() != null) {
                gdwjrq2 = new java.sql.Date(planissuedDto.getGdwjrq().getTime());
            }
            if (planissuedDto.getJhwc() != null) {
                jhwc2 = new java.sql.Date(planissuedDto.getJhwc().getTime());
            }
            if (planissuedDto.getSjwc() != null) {
                sjwc2 = new java.sql.Date(planissuedDto.getSjwc().getTime());
            }
            if (planissuedDto.getJhkg() != null) {
                jhkg2 = new java.sql.Date(planissuedDto.getJhkg().getTime());
            }
            planissuedDto.setJfrq(jfrq2);
            planissuedDto.setWcrq(wcrq2);
            planissuedDto.setGdwjrq(gdwjrq2);
            planissuedDto.setJhwc(jhwc2);
            planissuedDto.setSjwc(sjwc2);
            planissuedDto.setJhkg(jhkg2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return this.planIssuedMapper.Update(planissuedDto);
    }

    @Override
    public PlanIssued selectById(String objuid) {
        PlanIssued planIssued = this.planIssuedMapper.selectById(objuid);
        QueryWrapper wrapper = new QueryWrapper();
        planIssued.setEmployees(this.planEmployeeMapper.selectList(wrapper));
        return planIssued;
    }

}
